Report post

How to use lastinsertid in MySQL?

lastinsertid=$ (mysql -B -N -D "dbname" -e "insert into table1 (myvalue) values ('$ {foo}');select LAST_INSERT_ID ();") mysql -D "dbname" -e "insert into table2 (id_tab1) values ($ {lastinsertid});" LAST_INSERT_ID only available for the active connection. #mysq -command connect for every command.

How to retrieve last inserted ID in PHP?

The following examples are equal to the examples from the previous page ( PHP Insert Data Into MySQL ), except that we have added one single line of code to retrieve the ID of the last inserted record. We also echo the last inserted ID: echo "New record created successfully. Last inserted ID is: " . $last_id; echo "New record created successfully.

How do I add a lastinsertid to a table?

My solution is: lastinsertid=$ (mysql -B -N -D "dbname" -e "insert into table1 (myvalue) values ('$ {foo}');select LAST_INSERT_ID ();") mysql -D "dbname" -e "insert into table2 (id_tab1) values ($ {lastinsertid});"

What is a MySQL INSERT statement?

In the case of a multiple-row INSERT statement, it returns the first automatically generated value that was successfully inserted. Performing an INSERT or UPDATE statement using the LAST_INSERT_ID () MySQL function will also modify the value returned by mysqli_insert_id () .

Related articles

The World's Leading Crypto Trading Platform

Get my welcome gifts